package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shouhuobao.station.R;
import com.shouhuobao.take.model.ParcelBean;
import com.shouhuobao.take.utils.StringUtils;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;

/**
 * 代收包裹列表的适配器 <br>
 * Created by joker on 2017/5/25
 */
public class ParcelAdapter extends BaseAdapterExt<ParcelBean> {


    public ParcelAdapter(ArrayList<ParcelBean> items, Activity context) {
        super(items, context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            if (context instanceof ParcelCheckListActivity) {
                convertView = LayoutInflater.from(context).inflate(R.layout.take_parcel_item2, parent, false);
            } else {
                convertView = LayoutInflater.from(context).inflate(R.layout.take_parcel_item, parent, false);
            }
            holder.parcelDateTV = (TextView) convertView.findViewById(R.id.parceldateTV);
            holder.parcelCodeTV = (TextView) convertView.findViewById(R.id.parcelcodeTV);
            holder.postCodeTV = (TextView) convertView.findViewById(R.id.postcodeTV);
            holder.userInfoTV = (TextView) convertView.findViewById(R.id.userinfoTV);
            holder.takeTV = (TextView) convertView.findViewById(R.id.takeTV);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ParcelBean parcel = items.get(position);
        if (context instanceof ParcelCheckListActivity) {
            holder.parcelCodeTV.setText(parcel.getStorageCode());
        } else {
            holder.parcelCodeTV.setText(context.getResources().getString(R.string.take_bag_name_desc) + "：" + parcel.getStorageCode());
        }
        holder.postCodeTV.setText(parcel.getExpressCompanyName() + " : " + StringUtils.splitNumString(2, parcel.getExpressNumber()));
        if (StringUtils.isEmpty(parcel.getReceiverName())) {
            holder.userInfoTV.setText("用户信息: " + StringUtils.splitNumString(1, parcel.getReceiverPhone()));
        } else {
            holder.userInfoTV.setText("用户信息: " + parcel.getReceiverName() + " " + StringUtils.splitNumString(1, parcel.getReceiverPhone()));
        }

        // 不显示
        if (parcel.getStatus() <= 1) {
            holder.takeTV.setText("待取货");
            holder.parcelDateTV.setText(parcel.getCreateTime().substring(0, 16));
        } else if (parcel.getStatus() == 2) {
            holder.takeTV.setText("已取走");
            holder.parcelDateTV.setText(parcel.getTakeTime());
        } else if (parcel.getStatus() == 3) {
            holder.takeTV.setText("已超期");
            holder.parcelDateTV.setText(parcel.getTakeTime());
        }
        return convertView;
    }

    class ViewHolder {
        TextView parcelDateTV;// 包裹到店时间
        TextView parcelCodeTV;// 包裹编号
        TextView postCodeTV;// 快递编号
        TextView userInfoTV;// 用户信息
        TextView takeTV;// 取走或超期按钮，再到店中不显示
    }
}
